package com.anyikang.volunteer.sos.volunteer.app.mine;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.MyApplication;
import com.anyikang.volunteer.sos.volunteer.app.MyConstant;
import com.anyikang.volunteer.sos.volunteer.app.bean.User;
import com.anyikang.volunteer.sos.volunteer.utils.MyErrorDeal;
import com.anyikang.volunteer.sos.volunteer.utils.PageUtils;
import com.anyikang.volunteer.sos.volunteer.utils.ToastUtils;
import com.anyikang.volunteer.sos.volunteer.utils.okhttputils_callback.StringDialogCallback;
import com.lzy.okhttputils.OkHttpUtils;

import org.json.JSONObject;

import okhttp3.Request;
import okhttp3.Response;


public class ModifyPasswordActivity extends AppCompatActivity implements View.OnClickListener {

   private EditText edtOldPwd;
   private EditText edtNewPwd;
   private EditText edtNewPwd2;
   private Button btModifyPwd;
    private String TAG = "ModifyPasswordActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        initView();
    }

    public void initView()
    {
        TextView tvTitle = (TextView) this.findViewById(R.id.tv_title);
        tvTitle.setText("修改密码");

        ImageButton imgBtLeft = (ImageButton) this.findViewById(R.id.imgBtLeft);
        imgBtLeft.setOnClickListener(this);

        edtOldPwd = (EditText) this.findViewById(R.id.edtOldPwd);
        edtNewPwd = (EditText) this.findViewById(R.id.edtNewPwd);
        edtNewPwd2 = (EditText) this.findViewById(R.id.edtNewPwd2);
        btModifyPwd = (Button) this.findViewById(R.id.btModifyPwd);
        btModifyPwd.setOnClickListener(this);
        return;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.imgBtLeft:
                ModifyPasswordActivity.this.finish();
                break;
            case R.id.btModifyPwd:
                submitModifyPwd();
                break;
        }
    }



    public void dealLoginOut()
    {
        ((MyApplication)this.getApplication()).exit();
        User user = new User();
        user.userLogOut(this,0);  //退出登录时，不清用户名和密码
        finish();
        PageUtils.gotoLogin(this);
    }
    /**
     * 提交修改密码
     */

    private void submitModifyPwd() {

        String mOldPwd = edtOldPwd.getText().toString().trim();
        String mNewPwd = edtNewPwd.getText().toString().trim();
        String mNewPwd2 = edtNewPwd2.getText().toString().trim();

        if (TextUtils.isEmpty(mOldPwd)) {
            ToastUtils.showToast(ModifyPasswordActivity.this,"原密码不能为空！");
            return;
        }

        if (TextUtils.isEmpty(mNewPwd) || TextUtils.isEmpty(mNewPwd2)) {
            ToastUtils.showToast(ModifyPasswordActivity.this,"新密码不能为空！");
            return;
        }
        if (!mNewPwd.equals(mNewPwd2)) {
            ToastUtils.showToast(ModifyPasswordActivity.this,"两次密码输入不一致,请重新输入！");
            edtNewPwd2.setText("");
            edtNewPwd2.setFocusable(true);
            return;
        }


        User user = new User(ModifyPasswordActivity.this);
        String token = user.getUserToken();

        Log.i(TAG, MyConstant.API_MODIFY_PWD+"|tokenId="+token+",oldPassword="+mOldPwd+",newPassword="+mNewPwd+",newPassword2="+mNewPwd2);
        OkHttpUtils.post(MyConstant.API_MODIFY_PWD)//
                .tag(MyConstant.API_MODIFY_PWD)//
                .params("tokenId", token)//
                .params("oldPassword", mOldPwd)//
                .params("newPassword",mNewPwd)//
                .params("newPassword2",mNewPwd2)//
                .execute(new StringDialogCallback(ModifyPasswordActivity.this) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {


                        int retCode = response.code();
                        if(retCode == 200)
                        {
                            Log.i(TAG,"onSuccess 200 return："+s);
                            try
                            {
                                JSONObject registResult = new JSONObject(s);

                                if(registResult.getString("status").equals("1"))//修改密码成功
                                {
                                    ToastUtils.showToast(ModifyPasswordActivity.this,"修改成功");
                                    dealLoginOut();



                                }
                                else {  //修改失败
                                    String msg = registResult.getString("msg");
                                    Toast.makeText(ModifyPasswordActivity.this,msg,Toast.LENGTH_LONG).show();
                                }
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        }
                        else
                        {
                            Log.i(TAG,"onSuccess "+retCode+"return: "+s);
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, okhttp3.Call call, @Nullable Response response, @Nullable Exception e) {
                        MyErrorDeal.dealHttpError(response,ModifyPasswordActivity.this);
                    }




                });



    }
}
